/*
 * NewActor.java
 *
 * Created on September 4, 2008, 5:48 PM
 */

package cl.b9.socialNetwork.gui;

import cl.b9.socialNetwork.SNDirector;
import cl.b9.socialNetwork.model.SNActor;
import cl.b9.socialNetwork.model.SNActorFamily;
import cl.b9.socialNetwork.model.SNNode;
import java.awt.geom.Point2D;
import java.sql.SQLException;
import java.util.Set;
import javax.swing.ImageIcon;
import org.apache.log4j.Logger;

/**
 * Dialogo para la construcci&oacute;n y edici&oacute;n de un actor
 * @author  manuel
 */
public class ActorDialog extends javax.swing.JDialog {
    private Point2D p;
    private static Logger logger = Logger.getLogger(ActorDialog.class);
    private SNActorFamily actorType;
    private SNNode node;
    private enum Mode {NORMAL, EDIT, JOIN};
    private Mode mode = Mode.NORMAL;
    private Set<SNActor> picked;

    /**
     * Presenta un cuadro de dialogo que permite modificar el nodo
     * @param node
     */
    public ActorDialog(SNNode node) {
        super();
        this.mode = Mode.EDIT;
        logger.debug("node : " + node);
        this.p = node.getPosition();
        initComponents();
        txtName.setText(node.getLabel());
        this.btnCreate.setText("Guardar cambios");
        this.node = node;
        this.setModal(true);
    }
    
    /** Creates new form NewActor */
    public ActorDialog
            (SNActorFamily type,Point2D p) {
        super();
        this.p = p;
        this.actorType = type;
        initComponents();
        txtName.requestFocus();
        this.jLabel1.setText("Nuevo actor - Familia: " + type);
        this.setModal(true);
    }

    public ActorDialog(Set<SNActor> picked, Point2D p) {
        super();
        this.picked = picked;
        this.p = p;
        this.actorType = ((SNActor)picked.toArray()[0]).getFamily();
        initComponents();
        this.mode = Mode.JOIN;
        this.jLabel1.setText("Los actores seleccionados serán fusionados en el siguiente actor :");
        txtName.setText(((SNActor)picked.toArray()[0]).getLabel());
        txtName.requestFocus();
        this.setModal(true);
        btnCreate.setText("Unir");
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fileChooser = new javax.swing.JFileChooser();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        txtName = new javax.swing.JTextField();
        btnCreate = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        fileChooser.setName("fileChooser"); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cl.b9.socialNetwork.SocialNetworksApp.class).getContext().getResourceMap(ActorDialog.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        txtName.setText(resourceMap.getString("txtName.text")); // NOI18N
        txtName.setName("txtName"); // NOI18N
        txtName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNameActionPerformed(evt);
            }
        });

        btnCreate.setMnemonic('c');
        btnCreate.setText(resourceMap.getString("btnCreate.text")); // NOI18N
        btnCreate.setName("btnCreate"); // NOI18N
        btnCreate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateActionPerformed(evt);
            }
        });

        btnCancel.setText(resourceMap.getString("btnCancel.text")); // NOI18N
        btnCancel.setName("btnCancel"); // NOI18N
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel2)
                        .addComponent(jLabel1)
                        .addComponent(txtName, javax.swing.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE)
                        .addComponent(jSeparator1))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnCreate)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancel)
                        .addGap(93, 93, 93)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnCreate))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
    this.dispose();
}//GEN-LAST:event_btnCancelActionPerformed

private void btnCreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateActionPerformed
    boolean valid = true;
    String label = txtName.getText().trim();
    if (label.length()==0){
        Popup.showError(this, "Nombre de actor no puede ser vacío");
        valid = false;
    }
    if (!valid){
        return;
    }
    try {
        if (mode == Mode.EDIT){
            node.setLabel(txtName.getText().trim());
        }
        if (mode == Mode.NORMAL){
            logger.debug("Crear actor " + label + " en " + p);
            SNDirector.getInstance().createActor(actorType,label,p);
        }
        if (mode == Mode.JOIN){
            logger.debug("Fusionar nodos ");
            SNDirector.getInstance().joinNodes(picked, label, p);
        }
    } catch(SQLException ex){
        logger.error("Error al crear el actor " + ex.getLocalizedMessage());
        Popup.showError(this, "Error al crear el actor \n" + ex.getLocalizedMessage());
        ex.printStackTrace();
    }
    this.dispose();
    
    
   
}//GEN-LAST:event_btnCreateActionPerformed

private void txtNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNameActionPerformed
    btnCreate.doClick();
}//GEN-LAST:event_txtNameActionPerformed

public static final int THUMB_WIDTH = 100;
public static final int THUMB_HEIGHT = 100;
private ImageIcon imageIcon;


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnCreate;
    private javax.swing.JFileChooser fileChooser;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField txtName;
    // End of variables declaration//GEN-END:variables

}
